package io.tec.cloud.algorithm.c03_v20220607;

import java.util.Objects;

public class Node<T> {

    T data;
    Node<T> next;

    public Node(T data) {
        this.data = data;
    }

    public static void print(Node head) {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.data + "-->");
            cur = cur.next;
        }
        System.out.println("Null");
    }

    public static Node copy(Node head) {
        Node originalNode = head;
        Node newHead = null;
        Node node = null;
        Node curr = null;
        int i = 0;
        while (originalNode != null) {
            node = new Node(originalNode.data);
            if (i == 0) {
                newHead = node;
                curr = node;
            } else {
                curr.next = node;
                curr = curr.next;
            }
            originalNode = originalNode.next;
            i ++;
        }
        return newHead;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Node<?> node = (Node<?>) o;
        return Objects.equals(data, node.data) && Objects.equals(next, node.next);
    }

    @Override
    public int hashCode() {
        return Objects.hash(data, next);
    }
}